package com.turing.data.scripts.contract.filter

import com.turing.data.engine.CombineLongText
import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import com.turing.common.util.FileUtils
import com.turing.common.util.StringUtils
import com.turing.data.engine.utils.RegexExtract

/**
 * 场景：资产管理合同
 * 版本：Version 1
 * 要素名称：封闭期
 */

class ClosedPeriod_30184_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        String REGEX_CLOSED_1 = /.*封闭期(:|：).*/

        def textPathMap = dependParams.textPathMap

        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                if(StringUtils.isEmpty(resultVOs.get(i).childTaskSn)){
//                    return  resultVOs
                    continue
                }
                def rawPath = textPathMap.get(resultVOs.get(i).childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def text0 = resultVOs[i].text
                String trueValue = CombineLongText.fetchNearbyText(resultVOs.get(i), rawText,20)

                if ( trueValue =~ REGEX_CLOSED_1  ){


                     def index1 = text0.indexOf("封闭期：")
                    if(index1 == -1){
                        index1 = text0.indexOf("封闭期:")
                    }
                    if (index1 == -1) {
                        trueValueSet.add(resultVOs.get(i))
                        return trueValueSet
                    }

                    if(index1 > -1){
                        def JSONTextMap = dependParams.JSONTextMap
                        def fileTextStr = ""
                        fileTextStr = JSONTextMap.get(resultVOs[i].childTaskSn)
                        def startOffset = index1+4
                        TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(resultVOs[i])
                        objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, 0, fileTextStr, rawText)
                        objectNew.text = text0.substring(index1+4)
                        objectNew.businessValue = text0.substring(index1+4)
                        List< TaskElementVO.DrawResultVO > result = new ArrayList<>()
                        result.add(objectNew)
                        return result
                    }
//                    trueValueSet.add(resultVOs.get(i))
//                    return trueValueSet
                }
            }
        }

        try{
            if(trueValueSet.size()==0){

                String REGEX0 = /资产管理计划的封闭期和开放期.{1,40}。/
                println dependParams.JSONTextMap.size()
                dependParams.JSONTextMap.forEach(
                        { childTaskSn, jsonText ->
                            RegexExtract.doRuleExtract(REGEX0,childTaskSn, dependParams, jsonText, trueValueSet)
                        }
                )

                if(trueValueSet.size()>0){
                    textPathMap = dependParams.textPathMap
                    def childTaskSn = trueValueSet[0].childTaskSn
                    def rawPath = textPathMap.get(childTaskSn)
                    def rawText = FileUtils.loadJson(rawPath)
                    def JSONTextMap = dependParams.JSONTextMap
                    def fileTextStr = ""
                    fileTextStr = JSONTextMap.get(childTaskSn)

                    def rawPath0 = textPathMap.get(childTaskSn)
                    def rawText0 = FileUtils.loadJson(rawPath0)
                    def text = trueValueSet[0].text

                    def startOffset = 0
                    def endOffset = 0
                    def keyWord1 = "资产管理计划的封闭期和开放期"
                    if(text.contains(keyWord1)){
                        startOffset = text.indexOf(keyWord1) + keyWord1.length()
                        endOffset = 0
                    }

                    TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(trueValueSet[0])
                    objectNew = TextUtils.correctPositionInfoByOffset(objectNew, startOffset, endOffset, fileTextStr, rawText0)
                    trueValueSet.clear()
                    trueValueSet.add(objectNew)
                    return trueValueSet
                }
            }
        }
        catch (Exception e){
            e.printStackTrace()
        }

        TaskElementVO.DrawResultVO defaultDrawResultVO = new TaskElementVO.DrawResultVO()
        defaultDrawResultVO.text = ""
        trueValueSet.add(defaultDrawResultVO)
        return  trueValueSet

    }

}
